package com.wn.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.dao.UserinfoDao;
import com.wn.entity.PageParam;
import com.wn.entity.Role;
import com.wn.entity.Userinfo;
import com.wn.service.UserinfoService;
import com.wn.utils.JwtUtil;
import com.wn.utils.Rsp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * (Userinfo)表服务实现类
 *
 * @author makejava
 * @since 2020-10-26 09:43:15
 */
@Service("userinfoService")
@Transactional(rollbackFor = Exception.class)
public class UserinfoServiceImpl implements UserinfoService{
    @Autowired
    private UserinfoDao userinfoDao;


    public PageInfo<Userinfo> searchSomeByPage(PageParam<Userinfo> pageParam) {

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Userinfo> newscategoryList=userinfoDao.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Userinfo> newscategoryPageInfo = new PageInfo<Userinfo>(newscategoryList);

            return newscategoryPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Userinfo> newscategoryList=userinfoDao.searchSomeByPage(pageParam.getModel());
            PageInfo<Userinfo> newscategoryPageInfo = new PageInfo<Userinfo>(newscategoryList);

            return newscategoryPageInfo;

        }

    }

    @Override
    public Rsp updateId(Userinfo newsorder) {
        return userinfoDao.update(newsorder)==1?Rsp.success(null).msg("修改成功"):Rsp.fail().msg("修改失败");
    }

    @Override
    public Rsp addOne(Userinfo newsorder) {
        newsorder.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        newsorder.setCardimage("https://wnbucket.oss-cn-shanghai.aliyuncs.com/1604315712450543fc439e9474bd20e47cf6cb1d26b75.jpg");
        newsorder.setCreatetime(new Date());
        return userinfoDao.addOne(newsorder)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

    @Override
    public Rsp deleteOneTrue(String id) {
        return userinfoDao.deleteOneTrue(id)==1 ? Rsp.success(null).msg("逻辑删除成功"):Rsp.fail().msg("逻辑删除失败");
    }

    /**
     * 登录
     * @return
     */
    @Override
    public Rsp userinfoLogin(Userinfo loginUserinfo) {
        Userinfo userinfo = userinfoDao.userinfoLogin(loginUserinfo);

        if(userinfo!=null){
            try {
                Role role = userinfoDao.selectRole(userinfo.getRoleid().toString());
                userinfo.setRoleSign(role.getRoleSign());
                return Rsp.success(JwtUtil.createToken(userinfo));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            return Rsp.fail().msg("登录失败：账号或密码错误");
        }

        return null;
    }

    @Override
    public Rsp selphone(String phone,String password) {

        return userinfoDao.selphone(phone,password)==1?Rsp.success("修改成功"):Rsp.fail().msg("修改失败");
    }

    @Override
    public Rsp forgetPassword(Userinfo userinfo) {
        int result = userinfoDao.update(userinfo);
        return result == 1?Rsp.success(result):Rsp.fail();
    }

    @Override
    public Userinfo queryById(String id) {
        return userinfoDao.queryById(id);
    }

}
